package com.tengju.bff.interfaces.manage.crm.dealer.assembler;

import com.tengju.bff.interfaces.manage.crm.dealer.vo.*;
import com.tengju.data.domain.model.user.UserApplayTypeEnum;
import com.tengju.data.domain.model.user.UserLevelEnum;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.user.domain.model.csm.dealer.CrmDealerInfo;
import com.tengju.user.domain.model.user.StarLevelEnum;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class CrmDealerVOAssembler {
    /**
     * @param crmDealerInfoList 经销商数据
     * @return 列表查询
     *
     */
    public static List<CrmDealerInfoVO> crmDealerInfoListToVOList(List<CrmDealerInfo> crmDealerInfoList){
        if (CollectionUtils.isEmpty(crmDealerInfoList)) {
            return new ArrayList<>();
        }
        List<CrmDealerInfoVO> crmDealerInfoVOList = new ArrayList<>();
        for (CrmDealerInfo crmDealerInfo : crmDealerInfoList) {
            CrmDealerInfoVO crmDealerInfoVO = new CrmDealerInfoVO();

            crmDealerInfoVO.setIdCode(crmDealerInfo.getIdCode());
            crmDealerInfoVO.setUserName(crmDealerInfo.getUserName()+"");
            crmDealerInfoVO.setNickName(crmDealerInfo.getNickName()+"");
            crmDealerInfoVO.setInviteCode(crmDealerInfo.getInviteCode());
            //用户等级
            crmDealerInfoVO.setLevel(crmDealerInfo.getLevel() == null ? "" : crmDealerInfo.getLevel().getLevelDesc());
            //升级方式
            crmDealerInfoVO.setApplyType(crmDealerInfo.getApplyType() == null ? "" : crmDealerInfo.getApplyType().getType());
            //服务商等级
            crmDealerInfoVO.setStarLevel(crmDealerInfo.getStarLevel() == null ? "":crmDealerInfo.getStarLevel().getDesc());
            crmDealerInfoVO.setUserCountry(crmDealerInfo.getUserCountry()+"");
            crmDealerInfoVO.setUserProvince(crmDealerInfo.getUserProvince()+"");
            crmDealerInfoVO.setUserCity(crmDealerInfo.getUserCity()+"");
            String levelChgAt = "";
            if (crmDealerInfo.getLevelChgAt() != null) {
                levelChgAt = DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_FORMAT, crmDealerInfo.getLevelChgAt());
            }
            crmDealerInfoVO.setLevelChgAt(levelChgAt);

            //上级信息
            DealerSupInfoVO dealerSupInfoVO = new DealerSupInfoVO();
            dealerSupInfoVO.setSupUserName(String.format("上级：%s%s",crmDealerInfo.getSupUserName(),
                    crmDealerInfo.getSupIdCode() == null ? "":"("+crmDealerInfo.getSupIdCode()+")"));
            dealerSupInfoVO.setAsscHkName(String.format("所属黑卡：%s%s",crmDealerInfo.getAsscHkName(),
                    crmDealerInfo.getAsscHkIdCode() == null ? "":"("+crmDealerInfo.getAsscHkIdCode()+")"));
            dealerSupInfoVO.setBlStar1Name(String.format("所属1星：%s%s",crmDealerInfo.getBlStar1Name(),
                    crmDealerInfo.getBlStar1IdCode() == null ? "":"("+crmDealerInfo.getBlStar1IdCode()+")"
                    ));
            dealerSupInfoVO.setBlStar2Name(String.format("所属2星：%s%s",crmDealerInfo.getBlStar2Name(),
                    crmDealerInfo.getBlStar2IdCode() == null ? "":"("+crmDealerInfo.getBlStar2IdCode()+")"
            ));
            dealerSupInfoVO.setBlStar3Name(String.format("所属3星：%s%s",crmDealerInfo.getBlStar3Name(),
                    crmDealerInfo.getBlStar3IdCode() == null ? "":"("+crmDealerInfo.getBlStar3IdCode()+")"
            ));
            dealerSupInfoVO.setBlStar4Name(String.format("所属4星：%s%s",crmDealerInfo.getBlStar4Name(),
                    crmDealerInfo.getBlStar4IdCode() == null ? "":"("+crmDealerInfo.getBlStar4IdCode()+")"
            ));
            dealerSupInfoVO.setBlEcName(String.format("所属EC：%s%s",crmDealerInfo.getBlEcName(),
                    crmDealerInfo.getBlEcIdCode() == null ? "":"("+crmDealerInfo.getBlEcIdCode()+")"
            ));
            dealerSupInfoVO.setBlSecName(String.format("所属SEC：%s%s",crmDealerInfo.getBlSecName(),
                    crmDealerInfo.getBlSecIdCode() == null ? "":"("+crmDealerInfo.getBlSecIdCode()+")"
            ));
            dealerSupInfoVO.setBlClubName(String.format("所属俱乐部：%s%s",crmDealerInfo.getBlClubName(),
                    crmDealerInfo.getBlClubId() == null ? "":"("+crmDealerInfo.getBlClubId()+")"));


            //区域
            DealerAreaInfoVO dealerAreaInfoVO = new DealerAreaInfoVO();
            dealerAreaInfoVO.setAreaHkCount(String.format("黑钻PLUS数:%s",crmDealerInfo.getAreaHkCnt()));
            dealerAreaInfoVO.setAreaSVIPCount(String.format("黑钻数:%s",crmDealerInfo.getAreaSvipCnt()));
            dealerAreaInfoVO.setAreaVVIPCount(String.format("白金数:%s",crmDealerInfo.getAreaVvipCnt()));
            dealerAreaInfoVO.setAreaVIPCount(String.format("粉卡数:%s",crmDealerInfo.getAreaVipCnt()));
            dealerAreaInfoVO.setAreaRuCount(String.format("注册会员数:%s",crmDealerInfo.getAreaRuCnt()));

            //直属客户
            DealerCustInfoVO dealerCustInfoVO = new DealerCustInfoVO();
            dealerCustInfoVO.setCustHkCount(String.format("黑钻PLUS数:%s",crmDealerInfo.getCustHkCnt()));
            dealerCustInfoVO.setCustSVIPCount(String.format("黑钻数:%s",crmDealerInfo.getCustSvipCnt()));
            dealerCustInfoVO.setCustVVIPCount(String.format("白金数:%s",crmDealerInfo.getCustVvipCnt()));
            dealerCustInfoVO.setCustVIPCount(String.format("粉卡数:%s",crmDealerInfo.getCustVipCnt()));
            dealerCustInfoVO.setCustRuCount(String.format("注册会员数：%s",crmDealerInfo.getCustRuCnt()));

            crmDealerInfoVO.setDealerSupInfoVO(dealerSupInfoVO);
            crmDealerInfoVO.setDealerAreaInfoVO(dealerAreaInfoVO);
            crmDealerInfoVO.setDealerCustInfoVO(dealerCustInfoVO);

            crmDealerInfoVOList.add(crmDealerInfoVO);

        }

        return crmDealerInfoVOList;
    }

    /**
     * @param crmDealerInfoList
     * @author kezunfa
     * @date 2021/4/23 15:27
     * @return 列表导出（所有字段平铺）
     */
    public static List<ExportCrmDealerInfoVO> exportCrmDealerInfoListToVOList(List<CrmDealerInfo> crmDealerInfoList){
        if (CollectionUtils.isEmpty(crmDealerInfoList)) {
            return new ArrayList<>();
        }
        List<ExportCrmDealerInfoVO> crmDealerInfoVOList = new ArrayList<>();
        for (CrmDealerInfo crmDealerInfo : crmDealerInfoList) {
            ExportCrmDealerInfoVO crmDealerInfoVO = new ExportCrmDealerInfoVO();

            crmDealerInfoVO.setIdCode(crmDealerInfo.getIdCode());
            crmDealerInfoVO.setUserName(crmDealerInfo.getUserName()+"");
            crmDealerInfoVO.setNickName(crmDealerInfo.getNickName()+"");
            //用户等级
            crmDealerInfoVO.setLevel(crmDealerInfo.getLevel() == null ? "" : crmDealerInfo.getLevel().getLevelDesc());
            //升级方式
            crmDealerInfoVO.setApplyType(crmDealerInfo.getApplyType() == null ? "" : crmDealerInfo.getApplyType().getType());
            //服务商等级
            crmDealerInfoVO.setStarLevel(crmDealerInfo.getStarLevel() == null ? "":crmDealerInfo.getStarLevel().getShowName());
            crmDealerInfoVO.setUserCountry(crmDealerInfo.getUserCountry()+"");
            crmDealerInfoVO.setUserProvince(crmDealerInfo.getUserProvince()+"");
            crmDealerInfoVO.setUserCity(crmDealerInfo.getUserCity()+"");
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm");
            String levelChgAt = "";
            if (crmDealerInfo.getLevelChgAt() != null) {
                levelChgAt = dtf.format(crmDealerInfo.getLevelChgAt());
            }
            crmDealerInfoVO.setLevelChgAt(levelChgAt);

            //上级信息
            DealerSupInfoVO dealerSupInfoVO = new DealerSupInfoVO();
            dealerSupInfoVO.setSupUserName(crmDealerInfo.getSupUserName());
            dealerSupInfoVO.setAsscHkName(crmDealerInfo.getAsscHkName());
            dealerSupInfoVO.setBlStar1Name(crmDealerInfo.getBlStar1Name());
            dealerSupInfoVO.setBlStar2Name(crmDealerInfo.getBlStar2Name());
            dealerSupInfoVO.setBlStar3Name(crmDealerInfo.getBlStar3Name());
            dealerSupInfoVO.setBlStar4Name(crmDealerInfo.getBlStar4Name());
            dealerSupInfoVO.setBlEcName(crmDealerInfo.getBlEcName());
            dealerSupInfoVO.setBlSecName(crmDealerInfo.getBlSecName());
            dealerSupInfoVO.setBlClubName(crmDealerInfo.getBlClubName());


            //区域
            DealerAreaInfoVO dealerAreaInfoVO = new DealerAreaInfoVO();
            dealerAreaInfoVO.setAreaHkCount(String.valueOf(crmDealerInfo.getAreaHkCnt()));
            dealerAreaInfoVO.setAreaSVIPCount(String.valueOf(crmDealerInfo.getAreaSvipCnt()));
            dealerAreaInfoVO.setAreaVVIPCount(String.valueOf(crmDealerInfo.getAreaVvipCnt()));
            dealerAreaInfoVO.setAreaVIPCount(String.valueOf(crmDealerInfo.getAreaVipCnt()));
            dealerAreaInfoVO.setAreaRuCount(String.valueOf(crmDealerInfo.getAreaRuCnt()));

            //直属客户
            DealerCustInfoVO dealerCustInfoVO = new DealerCustInfoVO();
            dealerCustInfoVO.setCustHkCount(String.valueOf(crmDealerInfo.getCustHkCnt()));
            dealerCustInfoVO.setCustSVIPCount(String.valueOf(crmDealerInfo.getCustSvipCnt()));
            dealerCustInfoVO.setCustVVIPCount(String.valueOf(crmDealerInfo.getCustVvipCnt()));
            dealerCustInfoVO.setCustVIPCount(String.valueOf(crmDealerInfo.getCustVipCnt()));
            dealerCustInfoVO.setCustRuCount(String.valueOf(crmDealerInfo.getCustRuCnt()));

            crmDealerInfoVO.setDealerSupInfoVO(dealerSupInfoVO);
            crmDealerInfoVO.setDealerAreaInfoVO(dealerAreaInfoVO);
            crmDealerInfoVO.setDealerCustInfoVO(dealerCustInfoVO);

            crmDealerInfoVOList.add(crmDealerInfoVO);

        }

        return crmDealerInfoVOList;
    }


    public static DealerSupInfoVO dealerInfoToSupInfo(CrmDealerInfo crmDealerInfo) {
        if (crmDealerInfo == null) {
            return null;
        }
        DealerSupInfoVO dealerSupInfoVO = new DealerSupInfoVO();
        dealerSupInfoVO.setSupUserName(String.format("上级：%s%s",crmDealerInfo.getSupUserName(),
                crmDealerInfo.getSupIdCode() == null ? "":"("+crmDealerInfo.getSupIdCode()+")"));
        dealerSupInfoVO.setAsscHkName(String.format("所属黑卡：%s%s",crmDealerInfo.getAsscHkName(),
                crmDealerInfo.getAsscHkIdCode() == null ? "":"("+crmDealerInfo.getAsscHkIdCode()+")"));
        dealerSupInfoVO.setBlStar1Name(String.format("所属1星：%s%s",crmDealerInfo.getBlStar1Name(),
                crmDealerInfo.getBlStar1IdCode() == null ? "":"("+crmDealerInfo.getBlStar1IdCode()+")"
        ));
        dealerSupInfoVO.setBlStar2Name(String.format("所属2星：%s%s",crmDealerInfo.getBlStar2Name(),
                crmDealerInfo.getBlStar2IdCode() == null ? "":"("+crmDealerInfo.getBlStar2IdCode()+")"
        ));
        dealerSupInfoVO.setBlStar3Name(String.format("所属3星：%s%s",crmDealerInfo.getBlStar3Name(),
                crmDealerInfo.getBlStar3IdCode() == null ? "":"("+crmDealerInfo.getBlStar3IdCode()+")"
        ));
        dealerSupInfoVO.setBlStar4Name(String.format("所属4星：%s%s",crmDealerInfo.getBlStar4Name(),
                crmDealerInfo.getBlStar4IdCode() == null ? "":"("+crmDealerInfo.getBlStar4IdCode()+")"
        ));
        dealerSupInfoVO.setBlEcName(String.format("所属EC：%s%s",crmDealerInfo.getBlEcName(),
                crmDealerInfo.getBlEcIdCode() == null ? "":"("+crmDealerInfo.getBlEcIdCode()+")"
        ));
        dealerSupInfoVO.setBlSecName(String.format("所属SEC：%s%s",crmDealerInfo.getBlSecName(),
                crmDealerInfo.getBlSecIdCode() == null ? "":"("+crmDealerInfo.getBlSecIdCode()+")"
        ));
        dealerSupInfoVO.setBlClubName(String.format("所属俱乐部：%s%s",crmDealerInfo.getBlClubName(),
                crmDealerInfo.getBlClubId() == null ? "":"("+crmDealerInfo.getBlClubId()+")"));
        return dealerSupInfoVO;
    }
}
